<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getgoodslist">
            <el-button slot="append" icon="el-icon-search" @click="getgoodslist"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodslist" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="120px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="100px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="200px">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini"></el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="deletegoods(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background>
      </el-pagination>

    </el-card>
  </div>
</template>

<script>
export default {
  name: "Goodslist",
  data(){
    return {
      goodslist: [],
      total: 0,
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      }
    }
  },
  created() {
    this.getgoodslist();
  },
  methods: {
    async getgoodslist(){
      const {data: res} = await this.$http.get("/goods",{params: this.queryInfo});
      // console.log(res);
      if(res.meta.status !== 200) return this.$message.error("商品列表获取失败");
      this.goodslist = res.data.goods;
      this.total = res.data.total;
      // this.$message.success("获取商品列表成功")
    },

    handleSizeChange(newsize){
      this.queryInfo.pagesize = newsize;
      this.getgoodslist();
    },
    handleCurrentChange(newpage){
      this.queryInfo.pagenum = newpage;
      this.getgoodslist();
    },
    addGoods(){
      this.$router.push("/goods/add")
    },
    deletegoods(id){
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.delete(`/goods/${id}`).then(res=>{
            if(res.data.meta.status===200){
              this.getgoodslist();
              return this.$message({
                type: 'success',
                message: '删除成功!'
              });
            }
            this.$message.error("删除失败")
            
          }).catch(err=>{
            console.log(err);
          })
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  },
}
</script>

<style scpoed>
  .el-card{
    margin-top: 30px;
  }
  .el-table{
    margin: 20px 0px;
  }

  
</style>